<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\common\model\Article;
use app\common\model\CustomerAddress;
use app\common\model\OildepotPrice;
use app\common\model\Qrcode;
use app\common\model\Seller;
use think\annotation\route\Get;
use think\annotation\route\Group;

#[Group("index")]
class Index extends Api
{
    protected $noNeedLogin = ['*'];

    #[Get('siteinfo')]
    public function siteinfo()
    {
        $loop_img=['https://cdn.uviewui.com/uview/swiper/swiper1.png','https://cdn.uviewui.com/uview/swiper/swiper2.png','https://cdn.uviewui.com/uview/swiper/swiper3.png'];
        $loop_txt='不用东奔西走，这里优惠都有，为您提供“一站式”综合性服务！';
        $about_us='“送品源”是一家综合性城市配送服务平台，结合大数据算法，实现智慧物流，智能配送，我们以诚信、合作共赢原则，为用户提供“一站式”综合性服务。';
        $this->success('',['loop_img'=>$loop_img,'loop_txt'=>$loop_txt,'about_us'=>$about_us]);
    }

    #[Get('qrcode')]
    public function qrcode()
    {
        $seller_id=$this->request->get('seller_id');
        $seller=Seller::find($seller_id);
        if(!$seller){
            $this->error('销售不存在！');
        }
        $qrcode=Qrcode::where(['type'=>'seller','foreign_key'=>$seller_id])->find();
        if(!$qrcode){
            $qrcode=new Qrcode();
            $qrcode->type='seller';
            $qrcode->foreign_key=$seller_id;
            $qrcode->save();
        }
        header('Content-Type:image/png');
        $file=root_path().'public/qrcode/'.$qrcode->id.'.png';
        if(file_exists($file)){
            echo file_get_contents($file);
            exit;
        }
        $config=[
            'appid'=>site_config("third.mp_appid"),
            'appsecret'=>site_config("third.mp_appsecret")
        ];
        $wechat=new \WeChat\Qrcode($config);
        $ticket = $wechat->create($qrcode->id)['ticket'];
        $url=$wechat->url($ticket);
        //背景图
        $img_back=file_get_contents(root_path().'public/qrcode/qrcode-bk.jpg');
        //贴图1
        $img_qrcode=file_get_contents($url);
        //贴图2
        $user=\app\common\model\User::find($seller->user_id);
        $img_avatar=file_get_contents($user->avatar);;
        //将贴图1与贴图2复制到背景图上
        $img=imagecreatefromstring($img_back);
        $qrcode=imagecreatefromstring($img_qrcode);
        $avatar=imagecreatefromstring($img_avatar);
        $qrcode_width=imagesx($qrcode);
        $qrcode_height=imagesy($qrcode);
        $avatar_width=imagesx($avatar);
        $avatar_height=imagesy($avatar);
        imagecopymerge($img,$qrcode, 235, 970, 0, 0, $qrcode_width, $qrcode_height, 100);
        imagecopymerge($img,$avatar, 65, 890+$qrcode_height+10, 0, 0, $avatar_width, $avatar_height, 100);
        $name=$seller->realname;
        //将$name复制到背景图上
        $font=root_path().'public/qrcode/font.ttf';
        $color=imagecolorallocate($img, 36, 137, 128);
        imagettftext($img, 30, 0, 90, 890+$qrcode_height+10+$avatar_height+10+30, $color, $font, $name);
        //保存图片到本地
        imagepng($img,$file);
        //输出图片
        echo file_get_contents($file);
        exit;
    }
}
